from django.db import models
from users.models import User
from goods.models import *


# 收货地址表
# receiver varchar(20)
# receiver_mobile varchar(11)
# receiver_addr varchar(100)
# user fk
class Addr(models.Model):
    receiver = models.CharField('收件人', max_length=20)
    receiver_mobile = models.CharField('手机号', max_length=11)
    receiver_addr = models.CharField('收货地址', max_length=100)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='当前用户')

    def __str__(self):
        return "用户%s的收货地址" % self.user.username

    class Meta:
        db_table = 'addr'
        verbose_name_plural = '收货地址'


# Create your models here.
# 订单信息表
# order_id  varchar(64) pk  '订单编号'
# user  关系fk   '下单用户'
# addr  '收货地址'
# total_count int '商品总数'
# total_amount  decimal(10,2)  '总价格'
# freight  decimal(10,2)  '运费'
# pay_method smallint  choices  '支付方式'
# (1, '支付宝'),
# (2,'微信支付'),
# (3,'银联支付'),
# (4,'货到付款')
# status smallint  '支付状态'
# 1-待支付，2-待发货，3-待收货，4-待评价， 5-完成
# created_time  订单创建时间


class OrderInfo(models.Model):
    # 支付方式
    PAY_METHOD = (
        (1, '支付宝'),
        (2, '微信支付'),
        (3, '银联支付'),
        (4, '货到付款')
    )

    # 支付状态
    PAY_STATUS = (
        (1, '待支付'),
        (2, '代发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成')
    )

    order_id = models.CharField('订单编号', max_length=64, primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='下单用户', related_name='orders')
    addr = models.ForeignKey(Addr, on_delete=models.CASCADE, verbose_name='收货地址')
    total_count = models.IntegerField('商品总数', default=0)
    total_amount = models.DecimalField('总价格', max_digits=10, decimal_places=2)
    freight = models.DecimalField('运费', max_digits=10, decimal_places=2)
    pay_method = models.SmallIntegerField('支付方式', default=1, choices=PAY_METHOD)
    status = models.SmallIntegerField('支付状态', default=1, choices=PAY_STATUS)
    create_time = models.DateTimeField('创建时间', auto_now_add=True)

    def __str__(self):
        return "用户%s: 订单%s" % (self.user.username, self.order_id)

    class Meta:
        db_table = 'order_info'
        verbose_name_plural = '订单信息表'


# 订单商品表
# sku 购买的商品  fk  '订单商品'
# count  int  '购买的数量'
# price decimal(10,2) '价格'
# order 所属订单
# comment text '评论信息'
# score  smallint  '评分'  默认5
# is_anonymous  '是否匿名评价'
# is_commented '是否评价'

class OrderGoods(models.Model):
    SCORES = (
        (1, '20分'),
        (2, '40分'),
        (3, '60分'),
        (4, '80分'),
        (5, '100分'),
    )
    sku = models.ForeignKey(SKU, on_delete=models.CASCADE, verbose_name='订单商品')
    count = models.IntegerField('购买数量', default=1)
    price = models.DecimalField('价格', max_digits=10, decimal_places=2)
    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name='所属订单', related_name='order_goods')
    comment = models.TextField('评论信息', default="")
    score = models.SmallIntegerField('评分', default=SCORES)
    is_anonymous = models.BooleanField('是否匿名评论', default=False)
    is_commented = models.BooleanField('是否评价', default=False)

    def __str__(self):
        return "订单%s: 商品%s" % (self.order.order_id, self.sku.name)

    class Meta:
        db_table = 'order_goods'
        verbose_name_plural = '订单商品'


# 支付信息
# trade_id   varchar(100) 唯一 可为空 '支付编号'
# order 订单fk
class Payment(models.Model):
    trade_id = models.CharField('支付编码', max_length=100, unique=True, null=True, blank=True)
    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name='订单')

    def __str__(self):
        return "订单%s: 支付编号%s" % (self.order.order_id, self.trade_id)

    class Meta:
        db_table = 'payment'
        verbose_name_plural = '支付信息'
